import {
  Controller,
  Get,
  Post,
  Body,
  UseGuards,
  Query,
  ParseIntPipe,
  Param,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { VoucherService } from './voucher.service';
import { UnlockResourceDto } from './dto/unlock-resource.dto';
import { VoucherUsageLog } from './entities/voucher-usage-log.entity';
import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard';
import { User } from '../../../common/decorators/user.decorator';
import { User as UserEntity } from '../../users/entities/user.entity';

/**
 * 代金券控制器
 */
@ApiTags('代金券管理')
@Controller('member/voucher')
@UseGuards(JwtAuthGuard)
export class VoucherController {
  constructor(private readonly voucherService: VoucherService) {}

  @ApiOperation({ summary: '使用代金券解锁资源' })
  @ApiResponse({ status: 201, description: '解锁成功', type: VoucherUsageLog })
  @Post('unlock')
  async unlockResource(
    @User() user: UserEntity,
    @Body() unlockDto: UnlockResourceDto,
  ): Promise<VoucherUsageLog> {
    return await this.voucherService.unlockResource(user.id, unlockDto);
  }

  @ApiOperation({ summary: '获取用户代金券使用记录' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量', example: 10 })
  @Get('usage')
  async getUsageHistory(
    @User() user: UserEntity,
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('limit', new ParseIntPipe({ optional: true })) limit = 10,
  ): Promise<{
    data: VoucherUsageLog[];
    total: number;
    page: number;
    limit: number;
  }> {
    return await this.voucherService.getUserUsageHistory(user.id, page, limit);
  }

  @ApiOperation({ summary: '获取用户在特定资源类型上的使用记录' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量', example: 10 })
  @Get('usage/:resourceType')
  async getUsageByResourceType(
    @User() user: UserEntity,
    @Param('resourceType') resourceType: string,
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('limit', new ParseIntPipe({ optional: true })) limit = 10,
  ): Promise<{
    data: VoucherUsageLog[];
    total: number;
    page: number;
    limit: number;
  }> {
    return await this.voucherService.getUserUsageByResourceType(user.id, resourceType, page, limit);
  }

  @ApiOperation({ summary: '检查用户是否已解锁特定资源' })
  @ApiResponse({ status: 200, description: '检查成功' })
  @ApiQuery({ name: 'resourceId', required: false, description: '资源ID' })
  @Get('check/:resourceType')
  async checkUnlockedResource(
    @User() user: UserEntity,
    @Param('resourceType') resourceType: string,
    @Query('resourceId', new ParseIntPipe({ optional: true })) resourceId?: number,
  ): Promise<{ hasUnlocked: boolean }> {
    const hasUnlocked = await this.voucherService.hasUnlockedResource(
      user.id,
      resourceType,
      resourceId,
    );
    return { hasUnlocked };
  }

  @ApiOperation({ summary: '获取用户在特定资源类型上的总消费' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Get('spent/:resourceType')
  async getTotalSpent(
    @User() user: UserEntity,
    @Param('resourceType') resourceType: string,
  ): Promise<{ totalSpent: number }> {
    const totalSpent = await this.voucherService.getUserTotalSpentOnResourceType(
      user.id,
      resourceType,
    );
    return { totalSpent };
  }

  @ApiOperation({ summary: '管理员获取所有代金券使用记录' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量', example: 10 })
  @Get('admin/usage')
  async getAllUsageHistory(
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('limit', new ParseIntPipe({ optional: true })) limit = 10,
  ): Promise<{
    data: VoucherUsageLog[];
    total: number;
    page: number;
    limit: number;
  }> {
    return await this.voucherService.getAllUsageHistory(page, limit);
  }

  @ApiOperation({ summary: '获取代金券使用统计' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Get('admin/statistics')
  async getUsageStatistics(): Promise<{
    totalUsage: number;
    totalAmount: number;
    resourceTypeStats: Array<{ resourceType: string; count: number; totalAmount: number }>;
  }> {
    return await this.voucherService.getUsageStatistics();
  }
}